package com.example.expandablelistview.fragmentdialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.example.expandablelistview.R;

public class CustomDialogFragment extends DialogFragment {
	NoticeDialogListener mListener;
	Bundle bundle;

	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			mListener = (NoticeDialogListener) activity;
		} catch (ClassCastException e) {
			String msg = " must implement NoticeDialogListener";
			throw new ClassCastException(activity.toString() + msg);
		}
	}

	public interface NoticeDialogListener {
		public void onDialogClick(DialogFragment dialog, String res);
	}

	public Dialog onCreateDialog(Bundle savedInstanceState) {
		bundle = getArguments();
		LayoutInflater inflater = getActivity().getLayoutInflater();
		final LinearLayout linear = (LinearLayout) inflater.inflate(
				R.layout.order, null);
		return new AlertDialog.Builder(getActivity())
				.setIcon(R.drawable.ic_launcher)
				.setTitle(bundle.getString("title")).setView(linear)
				.setPositiveButton("확인", new OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						EditText product = (EditText) linear
								.findViewById(R.id.product);
						EditText number = (EditText) linear
								.findViewById(R.id.number);
						CheckBox paymethod = (CheckBox) linear
								.findViewById(R.id.paymethod);
						StringBuilder sb = new StringBuilder("주문 정보\n");
						sb.append(product.getText() + " ");
						sb.append(number.getText() + "개 ");
						sb.append(paymethod.isChecked() ? "착불결제" : "");
						mListener.onDialogClick(CustomDialogFragment.this,
								sb.toString());
					}
				}).setNegativeButton("취소", new OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						mListener.onDialogClick(CustomDialogFragment.this,
								"주문을 취소했습니다.");
					}
				}).create();
	}
}
